/**
 * Created by Baggio on 2018-10-16.
 */
$(function () {
    getMediaClassify();
    $("#mediaAdd").click(function () {
        $("#form-media-add").ajaxSubmit({
            type: "POST",
            url: '/media/modifyMedia.html',
            data: $('#form-media-add').serialize(),// 你的formid
            dataType: "json",
            error: function (request) {
                alert("Connection error");
            },
            beforeSend: function () {
                console.log("begin...............");
                $(".loading").show();
            },
            success: function (data) {

                console.log(data);
                if (data.code != 0) {
                    layer.msg('操作成功!', {icon: 6, time: 1000}, function () {
                        console.log(data);
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }

            },
            complete: function (XMLHttpRequest, textStatus) {
                // alert('远程调用成功，状态文本值：'+textStatus);
                console.log("end...............");
                $(".loading").hide();
            },
        });

    })
});

function playMedia(url) {
    var bgm = document.getElementById('bgMusic');
    $("#bgMusic").attr("src", url);
    bgm.play();
}
function modifiyStaus(tid, status) {
    var postData = {tid: tid, status: status == 1 ? 0 : 1};
    console.log(postData);
    $.ajax({
        type: 'POST',
        data: postData,
        url: '/media/modifyMediaStatus.html',
        dataType: 'json',
        success: function (data) {
            if (data.code != 1) {
                alert(data.msg);
                return;
            }
            alert("设置成功！");
            window.location.reload();
        }
    })


}

function getMediaClassify() {
    var postData = {};
    $.ajax({
        type: 'POST',
        data: postData,
        url: '/mediaClassify/getMediaClassify.html',
        dataType: 'json',
        success: function (data) {
            if (data.code != 1) {
                alert(data.msg);
                return;
            }
            tids = [];
            var id = $("#classfiy").val();
            if (id != null && id != '' && id != undefined) {
                getTid(data.data, id);
            }
            var datas = JSON.stringify(data.data).replace(/name/g, "n").replace(/tid/g, "v").replace(/mediaClassifyList/g, "s");

            $.cxSelect.defaults.url = JSON.parse(datas);
            $.cxSelect.defaults.firstTitle='   ';
            $.cxSelect.defaults.emptyStyle = 'none';
            $('#mediaClassfiy').cxSelect({
                selects: ['country', 'state', 'city', 'region'],
                required: false,
                nodata: 'none'

            });
        }
    })

}

function getTid(data, id) {
    for (var i = 0; i < data.length; i++) {
        var tid = data[i].tid;
        tids.push(tid);
        if (data[i].mediaClassifyList != undefined && data[i].mediaClassifyList.length > 0) {
            getTid(data[i].mediaClassifyList, id);
        }
        console.log(id);
        if (data[i].tid == id) {

            $("[name=classify]:eq(0)").attr("data-value", tids[0]);
            $("[name=classify]:eq(1)").attr("data-value", tids[1]);
            if (tids.length > 2) {
                $("[name=classify]:eq(2)").attr("data-value", tids[2]);
            }
            if (tids.length > 3) {
                $("[name=classify]:eq(3)").attr("data-value", tids[3]);
            }


            return;
        }
        tids.splice(-1, 1);
    }
}
